# -*- conding:utf-8 -*-
import numpy as np
from algorithm.MGraph import MDWGraph
import random


class GEANT:
    def initial_GEANT_network(self, function_V_num):
        N = 37
        V = list(range(N))
        E = np.zeros((N, N))
        inf = 999999
        E[0][36] = 10
        E[0][9] = 10
        E[1][2] = 3
        E[1][36] = 4
        E[2][1] = 3
        E[2][3] = 1
        E[2][36] = 3
        E[3][2] = 1
        E[4][36] = 4
        E[4][5] = 1
        E[5][4] = 1
        E[5][7] = 1
        E[6][36] = 6
        E[6][13] = 8
        E[7][5] = 1
        E[7][8] = 5
        E[7][14] = 2
        E[7][35] = 10
        E[8][9] = 1
        E[8][12] = 1
        E[8][36] = 3
        E[8][7] = 5
        E[8][13] = 2
        E[9][10] = 2
        E[9][34] = 10
        E[9][0] = 10
        E[9][8] = 1
        E[9][11] = 1
        E[9][30] = 5
        E[10][9] = 2
        E[10][12] = 4
        E[11][9] = 1
        E[11][16] = 1
        E[11][20] = 1
        E[11][31] = 4
        E[12][10] = 4
        E[12][8] = 1
        E[13][36] = 4
        E[13][6] = 8
        E[13][14] = 2
        E[13][15] = 2
        E[13][17] = 2
        E[13][34] = 10
        E[13][35] = 10
        E[13][20] = 2
        E[13][16] = 1
        E[13][8] = 2
        E[14][13] = 2
        E[14][7] = 2
        E[14][15] = 1
        E[14][13] = 2
        E[15][14] = 1
        E[15][18] = 1
        E[15][13] = 2
        E[16][11] = 1
        E[16][13] = 1
        E[17][13] = 2
        E[17][18] = 1
        E[17][33] = 5
        E[17][22] = 1
        E[17][21] = 2
        E[18][17] = 1
        E[18][15] = 1
        E[18][19] = 1
        E[19][18] = 1
        E[19][25] = 3
        E[19][28] = 3
        E[19][24] = 1
        E[19][23] = 1
        E[20][11] = 1
        E[20][13] = 2
        E[20][21] = 1
        E[20][31] = 4
        E[21][20] = 1
        E[21][17] = 2
        E[21][33] = 5
        E[21][32] = 4
        E[21][31] = 4
        E[22][17] = 1
        E[22][23] = 1
        E[23][22] = 1
        E[23][19] = 1
        E[23][26] = 2
        E[24][19] = 1
        E[25][19] = 3
        E[25][28] = 1
        E[25][29] = 4
        E[26][23] = 2
        E[27][28] = 1
        E[28][25] = 1
        E[28][29] = 4
        E[28][33] = 2
        E[28][27] = 1
        E[28][19] = 3
        E[29][25] = 4
        E[29][28] = 4
        E[30][31] = 2
        E[30][9] = 5
        E[31][30] = 2
        E[31][11] = 4
        E[31][20] = 4
        E[31][21] = 4
        E[32][21] = 4
        E[33][21] = 5
        E[33][17] = 5
        E[33][28] = 2
        E[34][9] = 10
        E[34][13] = 10
        E[35][13] = 10
        E[35][7] = 10
        E[36][1] = 4
        E[36][2] = 3
        E[36][4] = 4
        E[36][6] = 6
        E[36][13] = 4
        E[36][8] = 3
        E[36][0] = 10
        for i in range(len(E)):
            for j in range(len(E)):
                if i != j and E[i][j] == 0:
                    E[i][j] = E[j][i] = inf
        initial_Graph = MDWGraph(V, E)

        # 随机产生功能结点
        function_V = []
        while True:
            if len(function_V) < function_V_num:
                F_V = random.randint(0, N - 1)
                if F_V not in function_V:
                    function_V.append(F_V)
            else:
                break

        # 随机产生的边资源限制
        E_constrains = np.zeros((N, N))
        for i in range(len(E)):
            for j in range(i + 1, len(E)):
                if E[i][j] != inf and i != j:
                    E_constrains[i][j] = E_constrains[j][i] = random.randint(9000, 10000)

        # 随机产生功能结点限制
        function_V_constrains = []
        for i in range(function_V_num):
            function_V_constrains.append(random.randint(4000, 12000))

        neibor_function_v = []
        neibor_function_v.append(function_V[random.randint(0, len(function_V) - 1)])
        v = function_V[random.randint(0, len(function_V) - 1)]
        while v in neibor_function_v:
            v = function_V[random.randint(0, len(function_V) - 1)]
        neibor_function_v.append(v)
        v1 = neibor_function_v[0]
        v2 = neibor_function_v[1]
        # for i in range(initial_Graph.vnum):
        #     initial_Graph.E[i][v1] = initial_Graph.E[v1][i] = inf
        #     initial_Graph.E[i][v2] = initial_Graph.E[v2][i] = inf

        for i in range(initial_Graph.vnum):
            if initial_Graph.E[i][v1] != 0 and initial_Graph.E[i][v1] != inf:
                initial_Graph.E[i][v1] = initial_Graph.E[v1][i] = 100

        # 输出随机产生的图
        print('随机产生的结点个数：', initial_Graph.vnum)
        print('产生结点集合为：', initial_Graph.V)
        print('产生的边集合为：')
        print(initial_Graph.E)
        print('产生的功能结点分别是：', function_V)
        print('随机产生的边资源限制:\n', E_constrains)
        print('随机产生功能结点限制:\n', function_V_constrains)
        return initial_Graph, function_V, E_constrains, function_V_constrains, neibor_function_v

    def get_route(self, parent, source, destination):
        v = int(source)
        route = []
        while True:
            route.append(v)
            v = int(parent[v][destination])
            if v == destination:
                route.append(v)
                break

        return route
